package com.yuncheng.spcyApi.controller;

import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.busService.BusService;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcyRsYwzdxx;
import com.yuncheng.spcyApi.entity.SpcySqsxBasic;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.service.ISpcyRsYwzdxxService;
import com.yuncheng.spcyApi.service.ISpcySqsxBaiscService;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.spcyApi.entity.SpcySqsxJcxm;
import com.yuncheng.spcyApi.service.ISpcySqsxJcxmService;

import com.yuncheng.spcyApi.vo.common.Result;
import com.yuncheng.spcyApi.vo.common.TreeVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 审评查验-现场检查项目
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Api(tags = "审评查验-现场检查项目接口")
@RestController
@RequestMapping("/api/spzx-spcy/spcySqsxJcxm")
public class SpcySqsxJcxmController extends BaseController<SpcySqsxJcxm, ISpcySqsxJcxmService> {

    private static final Logger log = LoggerFactory.getLogger(SpcySqsxJcxmController.class);

    @Resource
    private ISpcySqsxJcxmService spcySqsxJcxmService;

    @Resource
    private ISpcySqsxBaiscService spcySqsxBaiscService;

    @Resource
    private ISpcyRsYwzdxxService spcyRsYwzdxxService;

    @Resource
    private BusService busService;

    /** -------------------------------------------【 现场检查项目 - 功能 】--------------------------------------------------------- */

    @Log(title = "查询检查项目详情-通过id")
    @ApiOperation(value = "查询检查项目详情-通过id")
    @ApiImplicitParam(name = "id",value = "检查项目id",required = true)
    @GetMapping(value = "querySqsxJcxmByid")
    public HttpResult<?> querySqsxJcxmByid(@RequestParam(value = "id",defaultValue = "")String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("检查项目id不能为空");
        }

        SpcySqsxJcxm jcxm = spcySqsxJcxmService.getById(id);
        return HttpResult.ok(jcxm);
    }

    @Log(title = "保存-申请事项-检查项目")
    @ApiOperation(value = "保存-申请事项-检查项目")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "检查项目id",required = true),
            @ApiImplicitParam(name = "fjcjl",value = "审查结论",required = true),
            @ApiImplicitParam(name = "fscjl",value = "审查记录",required = true),
            @ApiImplicitParam(name = "fgjqk",value = "技术审查内容",required = true),
            @ApiImplicitParam(name = "fgjshjl",value = "技术审查结论",required = true)
    })
    @PutMapping(value = "updateSqsxJcxm")
    public HttpResult<?> updateSqsxJcxm(@RequestBody HashMap<String, String> buffer){
        Result result = busService.updateSqsxJcxm(buffer);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }
        return HttpResult.ok("修改成功");
    }

    @Log(title = "保存-药品注册-检查项目")
    @ApiOperation(value = "保存-药品注册-检查项目")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "检查项目id",required = true),
            @ApiImplicitParam(name = "fjcjl",value = "审查结论",required = true),
            @ApiImplicitParam(name = "fscjl",value = "审查记录",required = true),
            @ApiImplicitParam(name = "fgjqk",value = "技术审查内容",required = true),
            @ApiImplicitParam(name = "fgjshjl",value = "技术审查结论",required = true)
    })
    @PutMapping(value = "updateYpzcJcxm")
    public HttpResult<?> updateYpzcJcxm(@RequestBody HashMap<String, String> buffer){
        Result result = busService.updateYpzcJcxm(buffer);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }
        return HttpResult.ok("修改成功");
    }

    @Log(title = "保存-化妆品-检查项目")
    @ApiOperation(value = "保存-化妆品-检查项目")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "检查项目id",required = true),
            @ApiImplicitParam(name = "fjcjl",value = "审查结论",required = true),
            @ApiImplicitParam(name = "fscjl",value = "审查记录",required = true),
            @ApiImplicitParam(name = "fgjqk",value = "技术审查内容",required = true),
            @ApiImplicitParam(name = "fgjshjl",value = "技术审查结论",required = true)
    })
    @PutMapping(value = "updateHzpJcxm")
    public HttpResult<?> updateHzpJcxm(@RequestBody HashMap<String, String> buffer){
        Result result = busService.updateHzpJcxm(buffer);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }
        return HttpResult.ok("修改成功");
    }

    @Log(title = "保存-药品流通-检查项目")
    @ApiOperation(value = "保存-药品流通-检查项目")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "检查项目id",required = true),
            @ApiImplicitParam(name = "fjcjl",value = "审查结论",required = true),
            @ApiImplicitParam(name = "fscjl",value = "审查记录",required = true),
            @ApiImplicitParam(name = "fgjqk",value = "技术审查内容",required = true),
            @ApiImplicitParam(name = "fgjshjl",value = "技术审查结论",required = true)
    })
    @PutMapping(value = "updateYpltJcxm")
    public HttpResult<?> updateYpltJcxm(@RequestBody HashMap<String,String> jcxmVo){
        Result result = busService.updateYpltJcxm(jcxmVo);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }
        return HttpResult.ok("修改成功");
    }

    /**
     * 获取缺陷记录内容-通过申请事项id
     * @param sqsxId 申请事项id
     */
    @Log(title = "获取缺陷记录内容-通过申请事项id")
    @ApiOperation(value = "获取缺陷记录内容-通过申请事项id")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sqsxid",value = "申请事项id",required = true)
    })
    @GetMapping(value = "/getDefectRecordMap")
    public HttpResult<?> getDefectRecordMap(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxId){
        if (StringUtils.isEmpty(sqsxId)){
            return HttpResult.error("申请事项id不能为空");
        }

        Map map = new HashMap();

        String ywflCode = busService.getYwflCode(sqsxId);
        if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPSC)){
            // 药品生产
            Map jcxmXX = spcySqsxJcxmService.getJcxmXXInYpsc(sqsxId);
            map.putAll(jcxmXX);

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPZC)){
            // 药品注册
            Map jcxmXX = spcySqsxJcxmService.getJcxmXXInYpzc(sqsxId);
            map.putAll(jcxmXX);

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPJY)){
            // 药品流通
            Map jcxmXX = spcySqsxJcxmService.getJcxmXXInYplt(sqsxId);
            map.putAll(jcxmXX);

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YLQX)){
            // 医疗器械
            Map jcxmXX = spcySqsxJcxmService.getJcxmXXInYlqx(sqsxId);
            map.putAll(jcxmXX);

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_HZP)){
            // 化妆品
            Map jcxmXX = spcySqsxJcxmService.getJcxmXXInHzp(sqsxId);
            map.putAll(jcxmXX);

        }else {
            log.error("业务分类编码为空: 申请事项id{}",sqsxId);
        }

        return HttpResult.ok(map);
    }

    /**
     * 获取结论-通过申请事项id
     * @param sqsxId 申请事项id
     */
    @Log(title = "获取结论-通过申请事项id")
    @ApiOperation(value = "获取结论-通过申请事项id")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "sqsxid", name= "申请事项id",required = true)
    })
    @GetMapping(value = "/getJcConclusionMap")
    public HttpResult<?> getJcConclusionMap(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxId){
        Map<String,String> resultMap = new HashMap();
        if (StringUtils.isEmpty(sqsxId)){
            return HttpResult.error("申请事项id不能为空");
        }
        String ywflCode = "";
        String conclusions = "";
        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxId);
        if (sqsx != null && StringUtils.isNotBlank(sqsx.getFywflcode())){
            ywflCode = sqsx.getFywflcode();
        }

        if (StringUtils.isBlank(ywflCode) || ywflCode.equals(SpcyConstant.YWFL_CODE_YPSC)){
            // 药品生产
            resultMap = spcySqsxJcxmService.getJcConclusionInYpsc(sqsxId, ywflCode);

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPZC)){
            // 药品注册
            resultMap = spcySqsxJcxmService.getJcConclusionInYpzc(sqsxId, ywflCode);

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPJY)){
            // 药品流通
            resultMap = spcySqsxJcxmService.getJcConclusionInYplt(sqsxId, ywflCode);

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YLQX)){
            // 医疗器械
            resultMap = spcySqsxJcxmService.getJcConclusionInYlqx(sqsxId, ywflCode);

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_HZP)){
            // 化妆品
            resultMap = spcySqsxJcxmService.getJcConclusionInHzp(sqsxId, ywflCode);

        }else {
            log.error("业务分类编码为空: 申请事项id{}",sqsxId);
        }
//        map.put("fcyjl",conclusions);
        return HttpResult.ok(resultMap);
    }

    @Log(title = "获取综合评定结论-通过申请事项id")
    @ApiOperation(value = "获取综合评定结论-通过申请事项id")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "sqsxid", name= "申请事项id",required = true)
    })
    @GetMapping(value = "getComprehensiveConclusionsMap")
    public HttpResult<?> getComprehensiveConclusionsMap(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxId){
        if (StringUtils.isEmpty(sqsxId)){
            return HttpResult.error("申请事项id不能为空");
        }

        Map<String,String> map = new HashMap();

        String ywflCode = "";
        String conclusions = "";

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxId);
        if (sqsx == null){
            return HttpResult.error("申请事项不存在，请刷新重试");
        }

        if (sqsx != null && StringUtils.isNotBlank(sqsx.getFywflcode())){
            ywflCode = sqsx.getFywflcode();
        }

        if (StringUtils.isBlank(ywflCode) || ywflCode.equals(SpcyConstant.YWFL_CODE_YPSC)){
            // 药品生产
            map = spcySqsxJcxmService.getComprehensiveConclusionInYpsc(sqsx,ywflCode);

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPZC)){
            // 药品注册
            map = spcySqsxJcxmService.getComprehensiveConclusionInYpzc(sqsx,ywflCode);

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YPJY)){
            // 药品流通
            map = spcySqsxJcxmService.getComprehensiveConclusionInYplt(sqsx,ywflCode);

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_YLQX)){
            // 医疗器械
            map = spcySqsxJcxmService.getComprehensiveConclusionInYlqx(sqsx,ywflCode);

        }else if (ywflCode.equals(SpcyConstant.YWFL_CODE_HZP)){
            // 化妆品
            map = spcySqsxJcxmService.getComprehensiveConclusionInHzp(sqsx,ywflCode);

        }else {
            log.error("业务分类编码为空: 申请事项id{}",sqsxId);
        }

        return HttpResult.ok(map);
    }

    @Log(title = "获取检查项目类型树形结构-通过申请事项id")
    @ApiOperation(value = "获取检查项目类型树形结构-通过申请事项id")
    @ApiImplicitParam(name = "sqsxid",value = "申请事项id",required = true)
    @GetMapping("/queryJcxmTreeType")
    public HttpResult<?> queryJcxmTreeType(@RequestParam(value = "sqsxid",defaultValue = "") String sqsxid,
                                           @RequestParam(value = "type",defaultValue = "")String type,
                                           @RequestParam(value = "xmlx",defaultValue = "")String xmlx){
        String fywflcode = "";
        List<TreeVo> treeVos = new ArrayList<>();
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("申请事项id不能为空");
        }

        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);

        if (sqsx == null){
            return HttpResult.error("申请事项为空，请联系管理员！");
        }

        if (sqsx != null){
            fywflcode = sqsx.getFywflcode();
        }

        if (StringUtils.isBlank(fywflcode)){
            return HttpResult.error("申请事项-业务分类编码为空！请联系管理员！");
        }

        if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPSC)){
            treeVos = treeJxcmTypeListInYpsc(sqsxid);
        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPZC)){
            treeVos = treeJxcmTypeListInYpzc(sqsxid,type,sqsx);
        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPJY)){
            treeVos = treeJxcmTypeListInYplt(sqsxid,type);
        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YLQX)){
            treeVos = treeJxcmTypeListInYlqx(sqsxid,type);
        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_HZP)){
            if (StringUtils.isBlank(xmlx)){
                xmlx = SpcyConstant.JCXM_TYPE3_SJSC;
            }
            treeVos = treeJxcmTypeListInHzp(sqsxid,type,xmlx);
        }

        return HttpResult.ok(treeVos);
    }

    @Log(title = "获取检查项目数据-通过类型")
    @ApiOperation(value = "获取检查项目数据-通过类型")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sfqx",value = "是否有缺陷，(全部/all,有缺陷/yes,无缺陷/no)"),
            @ApiImplicitParam(name = "jsscqx",value = "是否有缺陷，有缺陷/yes")
    })
    @GetMapping(value = "queryJcxmList")
    public HttpResult<?> queryJcxmList(@RequestParam(value = "type1",defaultValue = "")String type1,
                                       @RequestParam(value = "type2",defaultValue = "")String type2,
                                       @RequestParam(value = "type3",defaultValue = "")String type3,
                                       @RequestParam(value = "type4",defaultValue = "")String type4,
                                       @RequestParam(value = "type5",defaultValue = "")String type5,
                                       @RequestParam(value = "sqsxid",defaultValue = "")String sqsxid,
                                       @RequestParam(value = "fjcjl",defaultValue = "")String fjcjl,
                                       @RequestParam(value = "fscjl",defaultValue = "")String fscjl,
                                       @RequestParam(value = "sfqx",defaultValue = "all")String sfqx,
                                       @RequestParam(value = "jsscqx",defaultValue = "all")String jsscqx,
                                       @RequestParam(value = "type",defaultValue = "")String type,
                                       @RequestParam(value = "xmlx",defaultValue = "")String xmlx,
                                       @RequestParam(value = "ftype4",defaultValue = "")String ftype4,
                                       @RequestParam(value = "ftype5",defaultValue = "")String ftype5,
                                       @RequestParam(value = "ftype6",defaultValue = "")String ftype6,
                                       @RequestParam(value = "ftype7",defaultValue = "")String ftype7,
                                       @RequestParam(value = "ftype8",defaultValue = "")String ftype8,
                                       @RequestParam(value = "fgjshjl",defaultValue = "")String fgjshjl,
                                       @RequestParam(value = "pageNo",defaultValue = "1")Integer pageNo,
                                       @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        if (StringUtils.isBlank(sqsxid)){
            throw new BusinessException("申请事项参数为空！");
        }

        if (StringUtils.isNotBlank(type3)) {
            type3 = org.apache.commons.lang.StringEscapeUtils.unescapeHtml(type3);
        }

        Page<SpcySqsxJcxm> page = new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<SpcySqsxJcxm> queryWrapper = new LambdaQueryWrapper<>();
        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        String fywflcode = "";
        if (sqsx != null){
            fywflcode = sqsx.getFywflcode();
        }
        if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPSC)) {
            queryWrapper.like(StringUtils.isNotBlank(type1), SpcySqsxJcxm::getFtype3, type1);
            queryWrapper.like(StringUtils.isNotBlank(type2), SpcySqsxJcxm::getFtype4, type2);
            queryWrapper.like(StringUtils.isNotBlank(type3), SpcySqsxJcxm::getFtype5, type3);
            queryWrapper.like(StringUtils.isNotBlank(type4), SpcySqsxJcxm::getFtype6, type4);
            queryWrapper.like(StringUtils.isNotBlank(type5), SpcySqsxJcxm::getFtype7, type5);
            queryWrapper.like(StringUtils.isNotBlank(ftype4),SpcySqsxJcxm::getFtype5,ftype4);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPZC)) {
            queryWrapper.like(StringUtils.isNotBlank(type1), SpcySqsxJcxm::getFtype3, type1);
            queryWrapper.like(StringUtils.isNotBlank(type2), SpcySqsxJcxm::getFtype4, type2);
            queryWrapper.like(StringUtils.isNotBlank(type3), SpcySqsxJcxm::getFtype5, type3);

            queryWrapper.like(StringUtils.isNotBlank(ftype4),SpcySqsxJcxm::getFtype4,ftype4);
            queryWrapper.like(StringUtils.isNotBlank(ftype5),SpcySqsxJcxm::getFtype5,ftype5);
            queryWrapper.like(StringUtils.isNotBlank(ftype6),SpcySqsxJcxm::getFtype6,ftype6);
            queryWrapper.like(StringUtils.isNotBlank(ftype7),SpcySqsxJcxm::getFtype7,ftype7);
            queryWrapper.like(StringUtils.isNotBlank(ftype8),SpcySqsxJcxm::getFtype8,ftype8);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPJY)) {
            queryWrapper.like(StringUtils.isNotBlank(type1), SpcySqsxJcxm::getFtype3, type1);
            queryWrapper.like(StringUtils.isNotBlank(type2), SpcySqsxJcxm::getFtype5, type2);

            queryWrapper.like(StringUtils.isNotBlank(ftype4),SpcySqsxJcxm::getFtype4,ftype4);
            queryWrapper.like(StringUtils.isNotBlank(ftype5),SpcySqsxJcxm::getFtype5,ftype5);
            queryWrapper.like(StringUtils.isNotBlank(ftype6),SpcySqsxJcxm::getFtype6,ftype6);
            queryWrapper.like(StringUtils.isNotBlank(ftype7),SpcySqsxJcxm::getFtype7,ftype7);
            queryWrapper.like(StringUtils.isNotBlank(ftype8),SpcySqsxJcxm::getFtype8,ftype8);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YLQX)) {
            queryWrapper.like(StringUtils.isNotBlank(type),SpcySqsxJcxm::getFtype2,type);

            if (type.equals(SpcyConstant.JCXM_TYPE_SPXM)) {
                queryWrapper.like(StringUtils.isNotBlank(type1), SpcySqsxJcxm::getFtype3, type1);
                queryWrapper.like(StringUtils.isNotBlank(type2), SpcySqsxJcxm::getFtype4, type2);
                queryWrapper.like(StringUtils.isNotBlank(type3), SpcySqsxJcxm::getFtype5, type3);
                queryWrapper.like(StringUtils.isNotBlank(type4), SpcySqsxJcxm::getFtype6, type4);
                queryWrapper.like(StringUtils.isNotBlank(type5), SpcySqsxJcxm::getFtype7, type5);
            }else {
                queryWrapper.like(StringUtils.isNotBlank(type1), SpcySqsxJcxm::getFtype3, type1);
                queryWrapper.like(StringUtils.isNotBlank(type2), SpcySqsxJcxm::getFtype5, type2);
                queryWrapper.like(StringUtils.isNotBlank(type3), SpcySqsxJcxm::getFtype6, type3);
                queryWrapper.like(StringUtils.isNotBlank(type4), SpcySqsxJcxm::getFtype7, type4);
                queryWrapper.like(StringUtils.isNotBlank(type5), SpcySqsxJcxm::getFtype8, type5);
            }

            queryWrapper.like(StringUtils.isNotBlank(ftype4),SpcySqsxJcxm::getFtype4,ftype4);
            queryWrapper.like(StringUtils.isNotBlank(ftype5),SpcySqsxJcxm::getFtype5,ftype5);
            queryWrapper.like(StringUtils.isNotBlank(ftype6),SpcySqsxJcxm::getFtype6,ftype6);
            queryWrapper.like(StringUtils.isNotBlank(ftype7),SpcySqsxJcxm::getFtype7,ftype7);
            queryWrapper.like(StringUtils.isNotBlank(ftype8),SpcySqsxJcxm::getFtype8,ftype8);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_HZP)) {

            queryWrapper.like(StringUtils.isNotBlank(xmlx), SpcySqsxJcxm::getFtype3, xmlx);
            queryWrapper.like(StringUtils.isNotBlank(type1), SpcySqsxJcxm::getFtype4, type1);
            queryWrapper.like(StringUtils.isNotBlank(type2), SpcySqsxJcxm::getFtype5, type2);

            queryWrapper.like(StringUtils.isNotBlank(ftype4),SpcySqsxJcxm::getFtype4,ftype4);
            queryWrapper.like(StringUtils.isNotBlank(ftype5),SpcySqsxJcxm::getFtype5,ftype5);
            queryWrapper.like(StringUtils.isNotBlank(ftype6),SpcySqsxJcxm::getFtype6,ftype6);
            queryWrapper.like(StringUtils.isNotBlank(ftype7),SpcySqsxJcxm::getFtype7,ftype7);
            queryWrapper.like(StringUtils.isNotBlank(ftype8),SpcySqsxJcxm::getFtype8,ftype8);

        }else if (fywflcode.equals(SpcyConstant.YWFL_CODE_YPZCLC)) {
            queryWrapper.like(StringUtils.isNotBlank(type1), SpcySqsxJcxm::getFtype3, type1);
            queryWrapper.like(StringUtils.isNotBlank(type2), SpcySqsxJcxm::getFtype4, type2);
            queryWrapper.like(StringUtils.isNotBlank(type3), SpcySqsxJcxm::getFtype5, type3);

            queryWrapper.like(StringUtils.isNotBlank(ftype4),SpcySqsxJcxm::getFtype4,ftype4);
            queryWrapper.like(StringUtils.isNotBlank(ftype5),SpcySqsxJcxm::getFtype5,ftype5);
            queryWrapper.like(StringUtils.isNotBlank(ftype6),SpcySqsxJcxm::getFtype6,ftype6);
            queryWrapper.like(StringUtils.isNotBlank(ftype7),SpcySqsxJcxm::getFtype7,ftype7);
            queryWrapper.like(StringUtils.isNotBlank(ftype8),SpcySqsxJcxm::getFtype8,ftype8);

        }

        // 是否有缺陷，(全部/all,有缺陷/yes,无缺陷/no)
        if (StringUtils.isNotBlank(sfqx)) {
            if (sfqx.equals("yes")) {
                String jcjl = SpcyConstant.getJcxmDefaultScjl(fywflcode);
                queryWrapper.ne(SpcySqsxJcxm::getFjcjl, jcjl);
            } else if (sfqx.equals("no")) {
                String jcjl = SpcyConstant.getJcxmDefaultScjl(fywflcode);
                queryWrapper.eq(SpcySqsxJcxm::getFjcjl, jcjl);
            }
        }

        // 是否有缺陷，(全部/all,有缺陷/yes,无缺陷/no)
        if (StringUtils.isNotBlank(jsscqx)){
            if (jsscqx.equals("yes")) {
                queryWrapper.isNotNull(SpcySqsxJcxm::getFgjqk);
            }else if (jsscqx.equals("no")){
                queryWrapper.isNull(SpcySqsxJcxm::getFgjqk);
            }
        }

        queryWrapper.like(StringUtils.isNotBlank(fjcjl),SpcySqsxJcxm::getFjcjl,fjcjl);
        queryWrapper.like(StringUtils.isNotBlank(fscjl),SpcySqsxJcxm::getFscjl,fscjl);
        queryWrapper.like(StringUtils.isNotBlank(fgjshjl),SpcySqsxJcxm::getFgjshjl,fgjshjl);
        queryWrapper.eq(SpcySqsxJcxm::getFsqsxid,sqsxid);
        queryWrapper.orderByAsc(SpcySqsxJcxm::getFxh);
        Page<SpcySqsxJcxm> jcxmPage = spcySqsxJcxmService.page(page, queryWrapper);
        return HttpResult.ok(jcxmPage);
    }

    /*****************************************【 检查项目 -- 相关类型 - 树形结构数据 】*************************************************/

    // 树形结构 - 药品生产 - 全部类型 -
    public List<TreeVo> treeJxcmTypeListInYpsc(String sqsxid){
        List<SpcySqsxJcxm> jcxmList = spcySqsxJcxmService.list(new LambdaQueryWrapper<SpcySqsxJcxm>()
                .eq(SpcySqsxJcxm::getFsqsxid, sqsxid)
                .orderByAsc(SpcySqsxJcxm::getFxh));

        List<TreeVo> list = new ArrayList<>();
        int i = 1;
        if (CollectionUtils.isNotEmpty(jcxmList)){
            List<String> type1List = jcxmList.stream().map(SpcySqsxJcxm::getFtype3).distinct().collect(Collectors.toList());

            for (String type1 : type1List){
                i = i +1;
                TreeVo treeVo = new TreeVo();
                treeVo.setTitle(type1);
                treeVo.setKey(String.valueOf(i));
                List<SpcySqsxJcxm> jcxmList2 = spcySqsxJcxmService.list(new LambdaQueryWrapper<SpcySqsxJcxm>()
                        .eq(SpcySqsxJcxm::getFsqsxid, sqsxid)
                        .eq(SpcySqsxJcxm::getFtype3,type1)
                        .orderByAsc(SpcySqsxJcxm::getFxh));

                List<String> type2List = jcxmList2.stream().map(SpcySqsxJcxm::getFtype4).distinct().collect(Collectors.toList());

                List<TreeVo> list2 = new ArrayList<>();
                for (String type2 : type2List){
                    i = i + 1;
                    TreeVo treeVo2 = new TreeVo();
                    treeVo2.setTitle(type2);
                    treeVo2.setKey(String.valueOf(i));
                    list2.add(treeVo2);
                }
                treeVo.setChildren(list2);
                list.add(treeVo);
            }
        }
        return list;
    }

    // 树形结构 - 医疗器械 - 全部类型 -
    public List<TreeVo> treeJxcmTypeListInYlqx(String sqsxid,String type){
        if (type.equals(SpcyConstant.JCXM_TYPE_SPXM)){
            List<TreeVo> treeVos = spJcxmTypeListInYlqx(sqsxid, type);
            return treeVos;
        }else {
            List<TreeVo> treeVos = cyJcxmTypeListInYlqx(sqsxid, type);
            return treeVos;
        }
    }

    // 树形结构 - 化妆品 - 全部类型 - [xmlx]：实际生产，委托生产
    public List<TreeVo> treeJxcmTypeListInHzp(String sqsxid,String type,String xmlx){
        List<SpcySqsxJcxm> jcxmList = spcySqsxJcxmService.list(new LambdaQueryWrapper<SpcySqsxJcxm>()
                .eq(SpcySqsxJcxm::getFsqsxid, sqsxid)
                .eq(StringUtils.isNotBlank(xmlx),SpcySqsxJcxm::getFtype3,xmlx)
                .orderByAsc(SpcySqsxJcxm::getFxh));

        List<TreeVo> list = new ArrayList<>();
        int i = 1;
        if (CollectionUtils.isNotEmpty(jcxmList)){
            List<String> type1List = jcxmList.stream().map(SpcySqsxJcxm::getFtype4).distinct().collect(Collectors.toList());

            for (String type1 : type1List){
                i = i + 1;
                TreeVo treeVo = new TreeVo();
                treeVo.setTitle(type1);
                treeVo.setKey(String.valueOf(i));

                List<SpcySqsxJcxm> jcxmList2 = spcySqsxJcxmService.list(new LambdaQueryWrapper<SpcySqsxJcxm>()
                        .eq(SpcySqsxJcxm::getFsqsxid, sqsxid)
                        .eq(SpcySqsxJcxm::getFtype4,type1)
                        .orderByAsc(SpcySqsxJcxm::getFxh));

                List<String> type2List = jcxmList2.stream().map(SpcySqsxJcxm::getFtype5).distinct().collect(Collectors.toList());

                List<TreeVo> list2 = new ArrayList<>();
                for (String type2 : type2List){
                    i = i + 1;
                    TreeVo treeVo2 = new TreeVo();
                    treeVo2.setTitle(type2);
                    treeVo2.setKey(String.valueOf(i));

                    list2.add(treeVo2);
                }
                treeVo.setChildren(list2);
                list.add(treeVo);
            }
        }
        return list;
    }

    // 树形结构 - 药品注册 - 全部类型 -
    public List<TreeVo> treeJxcmTypeListInYpzc(String sqsxid,String type,SpcySqsxBasic sqsx){

        // 0(注册) / 1(临床) / 2(未知)
        String flag = busService.verifyYpzcSqsxIsIfZc(sqsx);

        String zdlx = "";
        String zdnr = "";
        boolean isType3 = false;

        if (flag.equals("0")){
            zdlx = SpcyConstant.YPZC_XCJC_JCXM_DLQF_ZC;

        }else if(flag.equals("1")){
            zdlx = SpcyConstant.YPZC_XCJC_JCXM_DLQF_LC;
        }

        if (StringUtils.isNotBlank(zdlx)){
            // 获取相关的 注册/临床 现场项目大类
            List<SpcyRsYwzdxx> ywzd = spcyRsYwzdxxService.list(new LambdaQueryWrapper<SpcyRsYwzdxx>()
                    .eq(SpcyRsYwzdxx::getFzdlx1, zdlx)
                    .eq(SpcyRsYwzdxx::getFywflcode, SpcyConstant.YWFL_CODE_YPZC));
            if (CollectionUtils.isNotEmpty(ywzd)){
                isType3 = true;
                zdnr = ywzd.get(0).getFzdnr();
            }
        }

        List<SpcySqsxJcxm> jcxmList = spcySqsxJcxmService.list(new LambdaQueryWrapper<SpcySqsxJcxm>()
                .eq(SpcySqsxJcxm::getFsqsxid, sqsxid)
                .eq(StringUtils.isNotBlank(type),SpcySqsxJcxm::getFtype2,type)
                .orderByAsc(SpcySqsxJcxm::getFxh));

        List<TreeVo> list = new ArrayList<>();
        int i = 1;
        if (CollectionUtils.isNotEmpty(jcxmList)){
            List<String> type1List = jcxmList.stream().map(SpcySqsxJcxm::getFtype3).distinct().collect(Collectors.toList());

            for (String type1 : type1List){

                if (isType3){
                    if (StringUtils.isNotBlank(zdnr) && !zdnr.contains(type1)){
                        continue;
                    }
                }

                i = i + 1;
                TreeVo treeVo = new TreeVo();
                treeVo.setTitle(type1);
                treeVo.setKey(String.valueOf(i));

                List<SpcySqsxJcxm> jcxmList2 = spcySqsxJcxmService.list(new LambdaQueryWrapper<SpcySqsxJcxm>()
                        .eq(SpcySqsxJcxm::getFsqsxid, sqsxid)
                        .eq(SpcySqsxJcxm::getFtype3,type1)
                        .orderByAsc(SpcySqsxJcxm::getFxh));

                List<String> type2List = jcxmList2.stream().map(SpcySqsxJcxm::getFtype4).distinct().collect(Collectors.toList());

                List<TreeVo> list2 = new ArrayList<>();
                for (String type2 : type2List){
                    i = i + 1;
                    TreeVo treeVo2 = new TreeVo();
                    treeVo2.setTitle(type2);
                    treeVo2.setKey(String.valueOf(i));

                    List<SpcySqsxJcxm> jcxmList3 = spcySqsxJcxmService.list(new LambdaQueryWrapper<SpcySqsxJcxm>()
                            .eq(SpcySqsxJcxm::getFsqsxid, sqsxid)
                            .eq(SpcySqsxJcxm::getFtype4,type2)
                            .orderByAsc(SpcySqsxJcxm::getFxh));

                    List<String> type3List = jcxmList3.stream().map(SpcySqsxJcxm::getFtype5).distinct().collect(Collectors.toList());

                    List<TreeVo> list3 = new ArrayList<>();
                    for (String type3 : type3List){
                        i = i + 1;
                        TreeVo treeVo3 = new TreeVo();
                        treeVo3.setTitle(type3);
                        treeVo3.setKey(String.valueOf(i));

                        /*List<SpcySqsxJcxm> jcxmList4 = spcySqsxJcxmService.list(new LambdaQueryWrapper<SpcySqsxJcxm>()
                                .eq(SpcySqsxJcxm::getFsqsxid, sqsxid)
                                .eq(SpcySqsxJcxm::getFtype5,type3)
                                .orderByAsc(SpcySqsxJcxm::getFxh));

                        List<String> type4List = jcxmList4.stream().map(SpcySqsxJcxm::getFtype6).distinct().collect(Collectors.toList());

                        List<TreeVo> list4 = new ArrayList<>();
                        for (String type4 : type4List) {
                            i = i + 1;
                            TreeVo treeVo4 = new TreeVo();
                            treeVo4.setTitle(type4);
                            treeVo4.setKey(String.valueOf(i));
                            list4.add(treeVo4);
                        }
                        treeVo3.setChildren(list4);*/
                        list3.add(treeVo3);
                    }
                    treeVo2.setChildren(list3);
                    list2.add(treeVo2);
                }
                treeVo.setChildren(list2);
                list.add(treeVo);
            }
        }
        return list;
    }

    // 树形结构 - 药品流通 - 全部类型 -
    public List<TreeVo> treeJxcmTypeListInYplt(String sqsxid,String type){
        List<SpcySqsxJcxm> jcxmList = spcySqsxJcxmService.list(new LambdaQueryWrapper<SpcySqsxJcxm>()
                .eq(SpcySqsxJcxm::getFsqsxid, sqsxid).eq(SpcySqsxJcxm::getFsqsxid, sqsxid).eq(StringUtils.isNotBlank(type),SpcySqsxJcxm::getFtype2,type)
                .orderByAsc(SpcySqsxJcxm::getFxh));

        List<TreeVo> list = new ArrayList<>();
        int i = 1;
        if (CollectionUtils.isNotEmpty(jcxmList)){
            List<String> type1List = jcxmList.stream().map(SpcySqsxJcxm::getFtype3).distinct().collect(Collectors.toList());

            for (String type1 : type1List){
                i = i + 1;
                TreeVo treeVo = new TreeVo();
                treeVo.setTitle(type1);
                treeVo.setKey(String.valueOf(i));

                List<SpcySqsxJcxm> jcxmList2 = spcySqsxJcxmService.list(new LambdaQueryWrapper<SpcySqsxJcxm>()
                        .eq(SpcySqsxJcxm::getFsqsxid, sqsxid)
                        .eq(SpcySqsxJcxm::getFtype3,type1)
                        .orderByAsc(SpcySqsxJcxm::getFxh));

                List<String> type2List = jcxmList2.stream().map(SpcySqsxJcxm::getFtype5).distinct().collect(Collectors.toList());

                List<TreeVo> list2 = new ArrayList<>();
                for (String type2 : type2List){
                    i = i + 1;
                    TreeVo treeVo2 = new TreeVo();
                    treeVo2.setTitle(type2);
                    treeVo2.setKey(String.valueOf(i));

                    /*List<SpcySqsxJcxm> jcxmList3 = spcySqsxJcxmService.list(new LambdaQueryWrapper<SpcySqsxJcxm>()
                            .eq(SpcySqsxJcxm::getFsqsxid, sqsxid)
                            .isNotNull(SpcySqsxJcxm::getFtype4)
                            .eq(StringUtils.isNotBlank(type2),SpcySqsxJcxm::getFtype4,type2)
                            .orderByAsc(SpcySqsxJcxm::getFxh));

                    List<String> type3List = jcxmList3.stream().map(SpcySqsxJcxm::getFtype5).distinct().collect(Collectors.toList());*/

                    /*List<TreeVo> list3 = new ArrayList<>();
                    for (String type3 : type3List){
                        i = i + 1;
                        TreeVo treeVo3 = new TreeVo();
                        treeVo3.setTitle(type3);
                        treeVo3.setKey(String.valueOf(i));
                        list3.add(treeVo3);
                    }
                    treeVo2.setChildren(list3);*/
                    list2.add(treeVo2);
                }
                treeVo.setChildren(list2);
                list.add(treeVo);
            }
        }
        return list;
    }

    // 医疗器械-审评树形结构
    public List<TreeVo> spJcxmTypeListInYlqx(String sqsxid,String type){
        List<SpcySqsxJcxm> jcxmList = spcySqsxJcxmService.list(new LambdaQueryWrapper<SpcySqsxJcxm>()
                .eq(SpcySqsxJcxm::getFsqsxid, sqsxid).eq(StringUtils.isNotBlank(type),SpcySqsxJcxm::getFtype2,type)
                .orderByAsc(SpcySqsxJcxm::getFxh));

        List<TreeVo> list = new ArrayList<>();
        int i = 1;
        if (CollectionUtils.isNotEmpty(jcxmList)){
            List<String> type1List = jcxmList.stream().map(SpcySqsxJcxm::getFtype3).distinct().collect(Collectors.toList());

            for (String type1 : type1List){
                i = i + 1;
                TreeVo treeVo = new TreeVo();
                treeVo.setTitle(type1);
                treeVo.setKey(String.valueOf(i));

                List<SpcySqsxJcxm> jcxmList2 = spcySqsxJcxmService.list(new LambdaQueryWrapper<SpcySqsxJcxm>()
                        .eq(SpcySqsxJcxm::getFsqsxid, sqsxid)
                        .eq(SpcySqsxJcxm::getFtype3,type1)
                        .orderByAsc(SpcySqsxJcxm::getFxh));

                List<String> type2List = jcxmList2.stream().map(SpcySqsxJcxm::getFtype4).distinct().collect(Collectors.toList());

                List<TreeVo> list2 = new ArrayList<>();
                for (String type2 : type2List){
                    i = i + 1;
                    TreeVo treeVo2 = new TreeVo();
                    treeVo2.setTitle(type2);
                    treeVo2.setKey(String.valueOf(i));

                    List<SpcySqsxJcxm> jcxmList3 = spcySqsxJcxmService.list(new LambdaQueryWrapper<SpcySqsxJcxm>()
                            .eq(SpcySqsxJcxm::getFsqsxid, sqsxid)
                            .eq(SpcySqsxJcxm::getFtype4,type2)
                            .orderByAsc(SpcySqsxJcxm::getFxh));

                    List<String> type3List = jcxmList3.stream().map(SpcySqsxJcxm::getFtype5).distinct().collect(Collectors.toList());

                    List<TreeVo> list3 = new ArrayList<>();
                    for (String type3 : type3List){
                        i = i + 1;
                        TreeVo treeVo3 = new TreeVo();
                        treeVo3.setTitle(type3);
                        treeVo3.setKey(String.valueOf(i));
                        list3.add(treeVo3);
                    }
                    treeVo2.setChildren(list3);
                    list2.add(treeVo2);
                }
                treeVo.setChildren(list2);
                list.add(treeVo);
            }
        }
        return list;
    }

    // 医疗器械-查验树形结构
    public List<TreeVo> cyJcxmTypeListInYlqx(String sqsxid,String type){
        List<SpcySqsxJcxm> jcxmList = spcySqsxJcxmService.list(new LambdaQueryWrapper<SpcySqsxJcxm>()
                .eq(SpcySqsxJcxm::getFsqsxid, sqsxid).eq(StringUtils.isNotBlank(type),SpcySqsxJcxm::getFtype2,type)
                .orderByAsc(SpcySqsxJcxm::getFxh));

        List<TreeVo> list = new ArrayList<>();
        int i = 1;
        if (CollectionUtils.isNotEmpty(jcxmList)){
            List<String> type1List = jcxmList.stream().map(SpcySqsxJcxm::getFtype3).distinct().collect(Collectors.toList());

            for (String type1 : type1List){
                i = i + 1;
                TreeVo treeVo = new TreeVo();
                treeVo.setTitle(type1);
                treeVo.setKey(String.valueOf(i));

                List<SpcySqsxJcxm> jcxmList2 = spcySqsxJcxmService.list(new LambdaQueryWrapper<SpcySqsxJcxm>()
                        .eq(SpcySqsxJcxm::getFsqsxid, sqsxid)
                        .eq(SpcySqsxJcxm::getFtype3,type1)
                        .orderByAsc(SpcySqsxJcxm::getFxh));

                List<String> type2List = jcxmList2.stream().map(SpcySqsxJcxm::getFtype5).distinct().collect(Collectors.toList());

                List<TreeVo> list2 = new ArrayList<>();
                for (String type2 : type2List){
                    i = i + 1;
                    TreeVo treeVo2 = new TreeVo();
                    treeVo2.setTitle(type2);
                    treeVo2.setKey(String.valueOf(i));

                    List<SpcySqsxJcxm> jcxmList3 = spcySqsxJcxmService.list(new LambdaQueryWrapper<SpcySqsxJcxm>()
                            .eq(SpcySqsxJcxm::getFsqsxid, sqsxid)
                            .eq(SpcySqsxJcxm::getFtype5,type2)
                            .orderByAsc(SpcySqsxJcxm::getFxh));

                    List<String> type3List = jcxmList3.stream().map(SpcySqsxJcxm::getFtype6).distinct().collect(Collectors.toList());

                    List<TreeVo> list3 = new ArrayList<>();
                    for (String type3 : type3List){
                        i = i + 1;
                        TreeVo treeVo3 = new TreeVo();
                        treeVo3.setTitle(type3);
                        treeVo3.setKey(String.valueOf(i));
                        list3.add(treeVo3);
                    }
                    treeVo2.setChildren(list3);
                    list2.add(treeVo2);
                }
                treeVo.setChildren(list2);
                list.add(treeVo);
            }
        }
        return list;
    }

}
